package cn.ithers.blog.controller.user;

import cn.ithers.blog.dto.*;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.IUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 10:30
 * @description:
 */
@Slf4j
@Api(value = "用户", tags = "用户")
@RestController
@RequestMapping("/user")
public class UserApi {

    @Autowired
    private IUserService userService;

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户信息")
    @GetMapping("/user_info/{userId}")
    public ResponseResult getUserInfo(@ApiParam(value = "用户ID", required = true) @PathVariable("userId") String userId) {

        return userService.getUserInfo(userId);
    }

    /**
     * 修改用户信息
     * 允许用户修改的内容
     * 1.头像
     * 2.用户名（唯一的）
     * 3.密码（单独修改）
     * 4.签名
     * 5.用户email（唯一的，单独修改）
     *
     * @param updateInfoUserDTO
     * @return
     */
    @ApiOperation(value = "修改用户信息")
    @PutMapping("/user_info/{userId}")
    public ResponseResult updateUserInfo(@ApiParam(value = "用户ID", required = true) @PathVariable("userId") String userId,
                                         @RequestBody UpdateInfoUserDTO updateInfoUserDTO) {
        return userService.updateUserInfo(userId, updateInfoUserDTO);
    }

    /**
     * 获取用户列表
     * 需要管理权限
     *
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取用户列表")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/list")
    public ResponseResult listUsers(@ApiParam(value = "当前页码", required = true) @RequestParam("page") int page,
                                    @ApiParam(value = "每页显示条数", required = true) @RequestParam("pageSize") int pageSize) {
        return userService.listUsers(page, pageSize);
    }

    /**
     * 删除用户
     * 需要管理员的权限
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "删除用户", notes = "\n需要管理员权限")
    @PreAuthorize("@permission.admin()")
    @DeleteMapping("/{userId}")
    public ResponseResult deleteUser(@ApiParam(value = "用户ID", required = true) @PathVariable("userId") String userId) {
        //判断当前操作的用户是谁
        //根据用户的角色判断是否可以删除
        //todo:通过注解的方式实现
        return userService.deleteUserById(userId);
    }

    /**
     * 检查用户该Email是否已经注册了
     *
     * @param email 邮箱地址
     * @return SUCCESS-->已经注册了，FAILED-->没有注册
     */
    @ApiOperation("检查邮箱是否注册")
    @ApiResponses({
            @ApiResponse(code = 2000, message = "表示当前邮箱已注册"),
            @ApiResponse(code = 4000, message = "表示当前邮箱未注册")
    })
    @GetMapping("/email")
    public ResponseResult checkEmail(@ApiParam(value = "邮箱地址", required = true) @RequestParam("email") String email) {

        return userService.checkEmail(email);
    }

    /**
     * 检查用户名是否已经注册了
     *
     * @param userName
     * @return SUCCESS-->已经注册了，FAILED-->没有注册
     */
    @ApiOperation("检查用户名是否注册")
    @ApiResponses({
            @ApiResponse(code = 2000, message = "表示当前用户名已注册"),
            @ApiResponse(code = 4000, message = "表示当前用户名未注册")
    })
    @GetMapping("/user_name")
    public ResponseResult checkUserName(@ApiParam(value = "用户名", required = true) @RequestParam("userName") String userName) {
        return userService.checkUserName(userName);
    }

    /**
     * 修改邮箱
     * 1.必须登录
     * 2.新邮箱没有注册过
     * <p>
     * 用户步骤
     * 1.已经登录
     * 2.输入新邮箱地址
     * 3.获取验证码 type = update
     * 4.输入验证码
     * 5.提交数据
     * <p>
     * 需要提交的数据
     * 1.新邮箱地址
     * 2.验证码
     *
     * @return
     */
    @ApiOperation(value = "修改邮箱")

    @PutMapping("/email")
    public ResponseResult updateEmail(@ApiParam(value = "邮箱地址", required = true) @RequestParam("email") String email,
                                      @ApiParam(value = "邮箱验证码", required = true) @RequestParam("verify_code") String verifyCode) {

        return userService.updateEmail(email, verifyCode);
    }


}
